import json
import time

from flask  import current_app as app

def monitor(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        execution_time = end_time - start_time
        # 构建监控数据
        monitoring_data = {
            'function_name': func.__name__,
            'args': args,
            'kwargs': kwargs,
            'return_value': json.dumps(result),
            'execution_time': execution_time
        }
        # print(monitoring_data)
        socket_handler= app.config.get('socket_handler')
        # 通过 WebSocket 发送监控数据到前端
        socket_handler.send(monitoring_data)
        return result
    return wrapper

